package com.web709.controller;

import com.web709.common.R;
import com.web709.util.AliOSSUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@RestController
@Api(tags = "文件上传接口")
public class UploadController {

    @Autowired
    private AliOSSUtils aliOSSUtils;
    @PostMapping("/upload")
    @ApiOperation(value = "文件上传", notes = "上传文件到阿里云OSS")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "image", value = "文件信息", required = true, dataType = "MultipartFile"),
            @ApiImplicitParam(name = "token", required = false,paramType = "header",dataType = "String")
    })
    public R<String> upload(MultipartFile image) throws IOException {
        log.info("文件上传, 文件名: {}", image.getOriginalFilename());

        //调用阿里云OSS工具类进行文件上传
        String url = aliOSSUtils.upload(image);
        log.info("文件上传完成,文件访问的url: {}", url);

        return R.success(url);
    }

}
